package qs.ipeControls
{
	import flash.events.Event;
	
	import mx.controls.Label;
	import mx.controls.TextInput;
	import mx.controls.listClasses.BaseListData;
	import mx.controls.listClasses.IDropInListItemRenderer;
	
	import qs.ipeControls.classes.IPEBase;
	
	[Event(name="change", type="flash.events.Event")]
	[Event(name="enter", type="mx.events.FlexEvent")]
	[Event(name="textInput", type="flash.events.TextEvent")]
	
	[Event(name="COMPLETEEDIT", type="flash.events.Event")]
	
	public class IPENumericTextInput extends IPEBase implements IDropInListItemRenderer
	{
		public function IPENumericTextInput():void
		{
			super();
			//this.tabFocusEnabled = false;
			//this.tabChildren = false;			
			nonEditableControl = new Label();
			editableControl = new TextInput();;
			//TextInput(editableControl).restrict = "0-9-.";
			TextInput(editableControl).setStyle("textAlign", "right");
			TextInput(editableControl).setStyle("fontSize", 16);
			nonEditableControl.setStyle("fontSize", 16);
			
			TextInput(editableControl).setStyle("fontFamily", '돋움체, Arial');
			nonEditableControl.setStyle("fontFamily", '돋움체, Arial');
			
			TextInput(editableControl).setStyle("fontWeight", 'bold');
			nonEditableControl.setStyle("fontWeight", 'bold');
			
			editableControl.setStyle("borderStyle","solid"); 
			facadeEvents(editableControl,"change","enter","textInput","valueCommit", "COMPLETEEDIT");
			
		}
		
		override protected function commitEditedValue():void
		{
			var textVal:String = TextInput(editableControl).text.toString();
			var s:String = textVal;

			var re:RegExp = new RegExp(",", "g");
			s = s.replace(re,"");
			
			var s2:String = NumberUtil.NumberFormat(Number(s));
			TextInput(editableControl).text = s2;
			if(_dv==null) {
				s2 = s2 + "만원";
			}
			else if(_dv=="iy") {
				s2 = s2 + "%";
			} else if(_dv=="n") {
				s2 = s2 + "개월";
			} else if(_dv=="n1") {
				s2 = s2 + "년";
			} 
			
			Label(nonEditableControl).text = s2;
			//Label(nonEditableControl).text = TextInput(editableControl).text;
		}
		
		
		private function get textInput():TextInput {return TextInput(editableControl);}
		private function get label():Label {return Label(nonEditableControl);}
		
		public function get condenseWhite():Boolean {return textInput.condenseWhite;}
		public function set condenseWhite(value:Boolean):void {textInput.condenseWhite = value;}
		
		public var _dv:String;
		public function set dv(value:String):void {
			_dv = value;
		}
			
		
		public function set text(value:String):void
		{
/*			if(_dv==null) {
				value = value + "원";
			}
			else if(_dv=="iy") {
				value = value + "%";
			} else if(_dv=="n") {
				value = value + "개월";
			} else if(_dv=="n1") {
				value = value + "년";
			} */
			
			TextInput(editableControl).text = value;
			Label(nonEditableControl).text = value;
			

		}		
		public function get text():String {
			//var re:RegExp = new RegExp(",", "g");
			//return textInput.text.replace(re,"");
			return textInput.text 
		}
		
		
		public function set dataValue(val:String):void{
			var re:RegExp = new RegExp(",", "g");
			val = val.replace(re,"");
			var s2:String;
			if(isNaN(Number(val))) {
				textInput.text = "0"
			}
			else {
				s2 = NumberUtil.NumberFormat(Number(val));
				textInput.text = s2;
			}
			//TextInput(editableControl).text = textInput.text;
			TextInput(editableControl).text = s2;
			if(_dv==null) {
				s2 = s2 + "만원";
			}
			else if(_dv=="iy") {
				s2 = s2 + "%";
			} else if(_dv=="n") {
				s2 = s2 + "개월";
			} else if(_dv=="n1") {
				s2 = s2 + "년";
			} 
			Label(nonEditableControl).text = s2;					
		}
		
		public function get dataValue():String {
			var re:RegExp = new RegExp(",", "g");
			textInput.text = textInput.text.replace(re,"");
			if(isNaN(Number(textInput.text))) {
				textInput.text = "0"
			}
			return textInput.text;
			//return textInput.text 
		}
		
		public function get imeMode():String { return textInput.imeMode }
		public function set imeMode(value:String):void { textInput.imeMode = value;}
		public function get length():int { return textInput.length; }
		public function get listData():BaseListData { return textInput.listData; }
		public function set listData(value:BaseListData):void 
		{ 
			textInput.listData = value;
			label.listData = value;
		}
		
		public function get fontSizeLabel():int { return label.getStyle("fontSize"); }
		public function set fontSizeLabel(value:int):void { label.setStyle("fontSize", value); }
		
		public function get fontColor():String { return label.getStyle("color"); }
		public function set fontColor(value:String):void { label.setStyle("color", value); }
		
		public function get fontWeight():String { return label.getStyle("fontWeight"); }
		public function set fontWeight(value:String):void { label.setStyle("fontWeight", value); }		
		
		public function get maxChars():int { return textInput.maxChars; }
		public function set maxChars(value:int):void { textInput.maxChars = value; }
		
		public function get restrict():String { return textInput.restrict; }
		public function set restrict(value:String):void { textInput.restrict = value;}		
		
		public function get selectionBeginIndex():int { return textInput.selectionBeginIndex; }
		public function set selectionBeginIndex(value:int):void { textInput.selectionBeginIndex = value;}
		
		public function get selectionEndIndex():int { return textInput.selectionEndIndex; }
		public function set selectionEndIndex(value:int):void { textInput.selectionEndIndex = value;}
		
		public function setSelection(beginIndex:int, endIndex:int):void { textInput.setSelection(beginIndex,endIndex); }
	}
}